using System;
using System.Collections;
using System.Collections.Generic;
using TEngine;
using UnityEngine;

namespace GameLogic
{
    public class Ground
    {
        public TileDetails actuallytileDetails;

        public void Init(TileDetails tileDetails)
        {
            this.actuallytileDetails = tileDetails;
            RegisterToManager();
        }

        private void RegisterToManager()
        {
            if (GroundGizmosManager.Instance != null)
            {
                GroundGizmosManager.Instance.RegisterGround(this);
                Debug.Log($"Ground注册到管理器: {actuallytileDetails.girdX}, {actuallytileDetails.gridY}");
            }
            else
            {
                Debug.LogWarning("GroundGizmosManager未找到，地块调试信息将不会显示");
            }
        }

        public void OnDigInit()
        {
            actuallytileDetails.canDig = false;
            actuallytileDetails.canDropItem = false;
            actuallytileDetails.canPlaceFurniture = false;
            actuallytileDetails.isNPCObstacle = false;
            actuallytileDetails.sinceDuged = 5;
            actuallytileDetails.Wetness = 10;
            actuallytileDetails.Fertility = 10;

            Debug.Log($"地块挖掘初始化: X:{actuallytileDetails.girdX} Y:{actuallytileDetails.gridY} " +
                     $"肥沃度:{actuallytileDetails.Fertility} 湿润度:{actuallytileDetails.Wetness}");

            GameEvent.AddEventListener(ILoginUI_Event.OnTimerChange, UpdateTileStatus);
            SetVisibility(true);
        }

        public void SetVisibility(bool visible)
        {
            actuallytileDetails.isVisible = visible;
        }

        private void UpdateTileStatus()
        {
            if (actuallytileDetails.isVisible)
            {
                var now = DateTime.Now;

                if (actuallytileDetails.lastWetnessUpdate == DateTime.MinValue)
                    actuallytileDetails.lastWetnessUpdate = now;
                var timeSinceLastWetnessUpdate = (now - actuallytileDetails.lastWetnessUpdate).TotalSeconds;
                var wetnessDecreaseCount = (int)(timeSinceLastWetnessUpdate / 48);
                DecreaseWetness(wetnessDecreaseCount * 1);
                actuallytileDetails.lastWetnessUpdate = now.AddSeconds(-(timeSinceLastWetnessUpdate % 48));

                if (actuallytileDetails.lastFertilityUpdate == DateTime.MinValue)
                    actuallytileDetails.lastFertilityUpdate = now;
                var timeSinceLastFertilityUpdate = (now - actuallytileDetails.lastFertilityUpdate).TotalSeconds;
                var fertilityDecreaseCount = (int)(timeSinceLastFertilityUpdate / 144);
                DecreaseFertility(fertilityDecreaseCount * 1);
                actuallytileDetails.lastFertilityUpdate = now.AddSeconds(-(timeSinceLastFertilityUpdate % 144));
            }
        }

        public void AddWetness(float value)
        {
            actuallytileDetails.Wetness += value;
        }

        public void DecreaseWetness(int value)
        {
            actuallytileDetails.Wetness -= value;
        }

        public void AddFertility(int value)
        {
            actuallytileDetails.Fertility += value;
        }

        public void DecreaseFertility(int value)
        {
            actuallytileDetails.Fertility -= value;
        }

        public void Cleanup()
        {
            // 不需要销毁GameObject，清理事件监听即可
            GameEvent.RemoveEventListener(ILoginUI_Event.OnTimerChange, UpdateTileStatus);
        }
    }
}